package models.System.Recharge;

import models.System.Headline.SystemTitle;
import models.System.SQL.SQL;
import models.System.Systems.Systems;
import models.System.Headline.code;
import models.SystemModels.ShopRecharge;
import models.Models.ShopReharges;
import models.SystemModels.SystemRecharge;
import play.db.jpa.JPA;

import javax.persistence.Query;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-07-29.
 */
public class Recharge {

    private static models.interfaces.Systems systems = new Systems();

    public static boolean ShopReharge(ShopReharges shopReharges){
        if (shopReharges.name.isEmpty()){
            return  false;
        }
        if (shopReharges.name.isEmpty()){
            return  false;
        }
        if (shopReharges.name.isEmpty()){
            return  false;
        }

        try{
            StringBuffer sb = new StringBuffer();
            sb.append(SQL.SELECT);
            sb.append(SQL.SHOPRECHARGES);
            sb.append(String.format(" where user=%s",shopReharges.name));
            sb.append(SQL.DESC);
            Query query = JPA.em().createQuery(sb.toString());
            List<ShopRecharge> shopRechargeLists = query.getResultList();
            if (shopRechargeLists.isEmpty()){
                ShopRecharge shopRecharge = new ShopRecharge();
                shopRecharge.price=Double.parseDouble(shopReharges.price);
                shopRecharge.user=shopReharges.name;
                shopRecharge.Total_balance= Double.parseDouble(String.format("%s",(0+(code.Price*Integer.parseInt(shopReharges.price)*0.2))));
                shopRecharge.time = new Date().toString();
                shopRecharge.balance = Double.parseDouble(String.format("%s",(0+(code.Price*Integer.parseInt(shopReharges.price)*2))));
                shopRecharge.type = SystemTitle.SystemRechargeTitle;
                shopRecharge.Odd_Numbers = systems.ids("SR");
                shopRecharge.save();
                return  true;
            }
            else{
                ShopRecharge shopRecharge = new ShopRecharge();
                shopRecharge.price=Double.parseDouble(shopReharges.price);
                shopRecharge.user=shopReharges.name;
                shopRecharge.Total_balance= Double.parseDouble(String.format("%s",(code.Price*Integer.parseInt(shopReharges.price))));
                shopRecharge.time = new Date().toString();
                shopRecharge.balance = (shopRechargeLists.get(0).balance+(code.Price*Integer.parseInt(shopReharges.price)));
                shopRecharge.type =shopReharges.SystemTitle;
                shopRecharge.Odd_Numbers = systems.ids("SR");
                shopRecharge.save();
                return  true;
            }
        }catch (Exception ex){
            return false;
        }

    }


}
